//
//  TradePswViewController.m
//  BorrowMoney
//
//  Created by zainishenbian on 16/4/14.
//  Copyright © 2016年 zainishenbian. All rights reserved.
//

#import "TradePswViewController.h"
#import "ZFHeader.h"

@interface TradePswViewController ()<UITextFieldDelegate>

{
    NSMutableArray *pwdIndicatorArr;
}

@end

@implementation TradePswViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"设置交易密码";
    self.view.backgroundColor = [UIColor whiteColor];
    //设置交易密码文案提示
    _label = [[UILabel alloc] init];
    _label.text = @"请设置交易密码";
    _label.font = [UIFont systemFontOfSize:15];
    _label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_label];
    
    [_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(NAVGATIONBAR_HEIGHT + 20 + 60*SCALEH);
        make.left.right.mas_equalTo(0);
    }];
    //一个密码框的宽
    CGFloat width = (SCREEN_WIDTH - 40)/6;
    //密码输入框
    UIView *inputView = [[UIView alloc] init];
    inputView.backgroundColor = [UIColor whiteColor];
    inputView.layer.borderColor = [UIColor colorWithRed:.7 green:.7 blue:.7 alpha:1.].CGColor;
    inputView.layer.borderWidth = 1;
    [self.view addSubview:inputView];
    
    [inputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_label.mas_bottom).offset(50*SCALEW);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(width);
    }];
    //装小黑点密码的数组
    pwdIndicatorArr = [[NSMutableArray alloc] init];
    //输入框
    _pswTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    _pswTextField.delegate = self;
    _pswTextField.hidden = YES;
    _pswTextField.keyboardType = UIKeyboardTypeNumberPad;
    [_pswTextField becomeFirstResponder];
    [inputView addSubview:_pswTextField];
    
    //小黑点和分割线
    for (int i = 0; i < 6; i ++)
    {
        UILabel *dot = [[UILabel alloc] init];
        dot.backgroundColor = [UIColor blackColor];
        dot.layer.cornerRadius = 5;
        dot.clipsToBounds = YES;
        dot.hidden = YES;
        [inputView addSubview:dot];
        [pwdIndicatorArr addObject:dot];
        
        [dot mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo((width - 10)/2 + i*width);
            make.top.mas_equalTo((width - 10)/2);
            make.size.mas_equalTo(CGSizeMake(10, 10));
        }];
        
        UILabel *line = [[UILabel alloc] init];
        line.backgroundColor = [UIColor colorWithRed:.7 green:.7 blue:.7 alpha:1.];
        [inputView addSubview:line];
        
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo((i + 1)*width);
            make.top.mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(0.5f, width));
        }];
    }
    
    //注册监听事件：观察textfield值得变化
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(textFieldDidChangeValue:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:_pswTextField];
}
#pragma mark -------textField delegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField.text.length >= 6 && string.length)
    {
        //输入的字符个数大于6，则无法继续输入，返回NO表示禁止输入
        return NO;
    }
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",@"^[0-9]*$"];
    if (![predicate evaluateWithObject:string])
    {
        return NO;
    }
    NSString *totalString;
    if (string.length <= 0)
    {
        totalString = [textField.text substringToIndex:textField.text.length-1];
    }
    else
    {
        totalString = [NSString stringWithFormat:@"%@%@",textField.text,string];
    }
    [self setDotWithCount:totalString.length];
//    if (totalString.length == 6)
//    {
//        _button.selected = YES;
//        _button.enabled = YES;
//    }
    
    return YES;
}

- (void)setDotWithCount:(NSInteger)count
{
    for (UILabel *dot in pwdIndicatorArr)
    {
        dot.hidden = YES;
    }
    
    for (int i = 0; i< count; i++)
    {
        ((UILabel*)[pwdIndicatorArr objectAtIndex:i]).hidden = NO;
    }
}
#pragma mark ———— textfield监听事件
- (void)textFieldDidChangeValue:(NSNotification *)notification
{
    UITextField *pswTF = (UITextField *)[notification object];
    if (pswTF.text.length >= 6) {
        
        NSLog(@"事件处理----------%@",pswTF.text);
        [pswTF resignFirstResponder];
    }
    else
    {
//        [pswTF becomeFirstResponder];
    }
}

- (void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"tradePass"];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"tradePass"];
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
